package com.example.stock.controller;

import com.example.stock.service.DailyMonitorService;
import com.example.stock.util.Result;
import com.example.stock.util.ResultGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin(origins = "*")
@RequestMapping("dailyMonitor")
public class DailyMonitorController {

    @Autowired
    DailyMonitorService dailyMonitorService;

    @GetMapping
    public Result index(@RequestParam(required = false) String date) {
        List<String> keys = dailyMonitorService.getDailyMonitorMap().keySet().stream().toList();

        Map<String, Object> ret = new HashMap<>();
        ret.put("date", keys);

        if (date != null) {
            ret.put("data", dailyMonitorService.getDataByMonitorDate(date));
        } else {
            ret.put("data", dailyMonitorService.getDataByMonitorDate(keys.getFirst()));
        }

        return ResultGenerator.genSuccessResult(ret);
    }
}
